package com.sj.uabrowser.net;

import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.sj.uabrowser.BuildConfig;
import com.sj.uabrowser.app.Constant;
import com.sj.uabrowser.net.converter.GsonConverterFactory;
import com.sj.uabrowser.net.converter.GsonNewConverterFactory;
import com.sj.uabrowser.net.converter.GsonVideoConverterFactory;
import com.sj.uabrowser.net.proxy.ProxyHandler;
import com.umeng.message.common.Const;

import java.lang.reflect.Proxy;
import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class AuthResultClient {

    private static HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor()
            .setLevel(BuildConfig.LOGGER? HttpLoggingInterceptor.Level.BODY:HttpLoggingInterceptor.Level.NONE);
//.setLevel(HttpLoggingInterceptor.Level.BODY);
    private static OkHttpClient.Builder client = BuildConfig.API_ENV?new OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .addNetworkInterceptor(new StethoInterceptor())
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            :new OkHttpClient.Builder()
//            .cache(new Cache(new File(TApplication.getInstance().getCacheDir(),"httpcache"),10*1024*1024L))
            .addInterceptor(httpLoggingInterceptor)
            .addNetworkInterceptor(new StethoInterceptor())
            .connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true);


    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(getBaseUrl())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();

    private static Retrofit retrofit_tianqi = new Retrofit.Builder()
            .baseUrl(Constant.TIANQI_BASEURL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();


    private static Retrofit retrofit_mainnewfuncs = new Retrofit.Builder()
            .baseUrl(getMainNewFuncsUrl())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();

    private static Retrofit retrofit_video = new Retrofit.Builder()
            .baseUrl(Constant.VIDEOURL)
            .addConverterFactory(GsonVideoConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();

    private static Retrofit retrofit_2 = new Retrofit.Builder()
            .baseUrl(Constant.ReleaseBaseUrl)
            .addConverterFactory(GsonNewConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();

    public static Retrofit retrofit_0 = new Retrofit.Builder()
            .baseUrl(getBaseUrl0())
            .addConverterFactory(GsonNewConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();


    private static String getBaseUrl() {
        return Constant.BaseUrl;
    }

    private static String getMainNewFuncsUrl(){
        return Constant.MAINFUNCS;
    }

    private static String getBaseUrl0() {
        return Constant.BaseUrl0;
    }


    public static ServiceApi SERVICES() {
        return getService(ServiceApi.class);
    }

    public static ServiceApi PROXYSERVICES() {
        return getProxyService(ServiceApi.class);
    }

    public static <T> T getService(Class<T> tClass) {
        return retrofit.create(tClass);
    }

    public static <T> T getServiceVideo(Class<T> tClass) {
        return retrofit_video.create(tClass);
    }


    public static <T> T getService2(Class<T> tClass) {
        return retrofit_2.create(tClass);
    }
    public static <T> T getService_tianqi(Class<T> tClass) {
        return retrofit_tianqi.create(tClass);
    }

    public static <T> T getServiceMainNewFuncs(Class<T> tClass) {
        return retrofit_mainnewfuncs.create(tClass);
    }

    public static <T> T getService0(Class<T> tClass) {
        return retrofit_0.create(tClass);
    }

    public static <T> T getProxyService(Class<T> tClass) {
        T t = retrofit.create(tClass);
        return (T) Proxy.newProxyInstance(tClass.getClassLoader(), new Class<?>[]{tClass}, new ProxyHandler(t));
    }

    private static String getBaseUrl4() {
        return Constant.MAIN_URL;
    }

    private static Retrofit retrofit_4 = new Retrofit.Builder()
            .baseUrl(getBaseUrl4())
            .addConverterFactory(retrofit2.converter.gson.GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client.build())
            .build();

    public static <T> T getService4(Class<T> tClass) {
        return retrofit_4.create(tClass);
    }

    public static void getAccessToken(Consumer<? super Throwable> onError) {
//        AuthResultClient.SERVICES()
//                .GetAccessToken(Constant.Version, Constant.Platform, Constant.AppId, Constant.AppSecret)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        new Consumer<TokenModel>() {
//                            @Override
//                            public void accept(TokenModel model) throws Exception {
//                                SPUtil.put(TApplication.getInstance(), SPUtil.KEY_ACCESSTOKEN, model.getAccessToken());
//                            }
//                        }, onError);
    }

}
